import wx
import os.path as op


class FileDropTarget(wx.FileDropTarget):
    """ This object implements Drop Target functionality for Files """

    def __init__(self, obj):
        """ Initialize the Drop Target, passing in the Object Reference to
            indicate what should receive the dropped files """
        # Initialize the wxFileDropTarget Object
        wx.FileDropTarget.__init__(self)
        # Store the Object Reference for dropped files
        self.obj = obj

    def OnDropFiles(self, x, y, filenames):
        """ Implement File Drop """
        # For Demo purposes, this function appends a list of the files dropped at the end of the widget's text
        # Move Insertion Point to the end of the widget's text
        self.obj.SetInsertionPointEnd()
        # append a list of the file names dropped
        # self.obj.WriteText("%d file(s) dropped at %d, %d:\n" %
        #    (len(filenames), x, y))
        for file in filenames:
            if op.isdir(file):
                if not self.obj.IsMultiLine():
                    self.obj.SetValue(file)
                    break
                else:
                    self.obj.WriteText(file + '\n')
        # self.obj.WriteText('\n')
        return True


class RedirectText(object):
    def __init__(self, aWxTextCtrl):
        self.out = aWxTextCtrl

    def write(self, string):
        self.out.AppendText(string)
